<template>
  <canvas :id="canvasId" :style="canvasStyle" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"></canvas>
</template>

<script>
export default {
  name: 'ec-canvas',
  props: {
    canvasId: {
      type: String,
      default: 'defaultCanvasId'
    },
    ec: {
      type: Object,
      required: true
    },
    canvasStyle: {
      type: String,
      default: 'width: 100%; height: 300px;'
    }
  },
  data() {
    return {
      isTouch: false,
      touches: [],
    };
  },
  methods: {
    touchStart(e) {
      this.touches = e.touches;
      this.isTouch = true;
      if (this.ec.onInit) {
        this.ec.onInit(this.$refs.canvas, this.ec);
      }
    },
    touchMove(e) {
      if (!this.isTouch) return;
      this.touches = e.touches;
    },
    touchEnd(e) {
      if (!this.isTouch) return;
      this.isTouch = false;
      this.touches = e.touches;
    }
  },
  mounted() {
    if (this.ec.onInit) {
      this.ec.onInit(this.$refs.canvas, this.ec);
    }
  }
};
</script>

<style scoped>
/* 在此添加样式 */
</style>
